<?php
namespace app\mobile\controller;

use app\model\Theme; 
use app\model\Worker; 
use app\model\Fans; 

class WWechatgroupAdd extends Base
{
    public function W_wechatgroup_add()
    {
global $_G, $_P;
$fans = $this->checkuser();
$themeid = intval($_P['themeid']);
if ($themeid < 0) {
    return_msg('管理后台地址不正确');
}
$system = $_G['system'];
$theme = Theme::getThemeById($themeid);
$config = Theme::getConfig($themeid);
if (empty($theme['id'])) {
    return_msg('管理后台地址不正确');
}
$myworker = Worker::getThemeWorkerByOpenidNew($_G['openid'], $themeid);
if (!(!$system['is_multiple_merchants'] || $config['showshop'] == 0 || ($system['is_multiple_merchants'] && $config['showshop'] == $myworker['company']))) {
    $myworker = "";
}
if (empty($myworker) || $myworker['level']>3) {
    return_msg('您不是员工,无权访问');
}

$company = intval($_P['company']);
$op = !empty($_P['op']) ? $_P['op'] : 'last';

$msg = Worker::gethoutaistats();
if (!empty($msg)) {
	return_msg($msg);
}

$do = 'w_wechatgroup_add';
//load_func('tpl');
if (empty($config['workertuan'])) {
	$config['workertuan']=5;
}
$wx = $_G['platform']['jssdkconfig']; 

$myworker = Worker::getThemeWorkerByOpenidzhuce($_G['openid'],$themeid,1);
if ($system['is_multiple_merchants']) {
    $shoplist = db_select("SELECT * FROM " . tablename(t_shangjia) .
        " WHERE  state = 1 order by displayorder");
}

$url= $_G['siteroot'].$this->createMobileUrl('w_wechatgroup',array("soft"=>$theme['softkey'],"themeid"=>$themeid));
$uploadurl= $_G['siteroot'].$this->createMobileUrl('w_init',array("op"=>'upload',"themeid"=>$themeid,"soft"=>$theme['softkey'],"server_openid"=>$_P['server_openid']));
// echo $uploadurl;
if ($op=='upload') {
	$r=downloadFromWxServer($system,$_P['bid']);
	$r[0]['remotepath'] = str_replace('http://', 'https://', $r[0]['remotepath']);
	return_msg($r[0],'','ajax'); 
	exit;
}

if ($op == 'last') {
    $result = Fans::W_wechatgroup_get($system);
    $wlist = $result['wlist'];
    $wechatgroup = $result['wechatgroup'];
}

if ($op == 'save') {
    $result = Fans::W_wechatgroup_add($myworker);
    if ($result['error']==1){
        return_msg($result['msg'],'','error');
    }else{
        return_msg("保存成功!",$url,'success');
    }
}
$return_clicent = get_defined_vars();
return view('/yxworker/w_wechatgroup_add' , $return_clicent);

}
}